//
//  MeDetailTableViewController.m
//  糗事百科
//
//  Created by 奚超明 on 16/6/4.
//  Copyright © 2016年 奚超明. All rights reserved.
//

#import "MeDetailTableViewController.h"
#import "MeDetailTableViewCell.h"

@interface MeDetailTableViewController ()
/** 背景图 */
@property (nonatomic, weak) UIImageView *backImageView;
/** 记录当前背景图的序号 */
@property (nonatomic, assign) NSInteger page;
/** 模块数组 */
@property (nonatomic, strong) NSMutableArray *functionArr;
/** 每个模块对应的内容数组(暂时死值) */
@property (nonatomic, strong) NSMutableArray *contentArr;

@end

@implementation MeDetailTableViewController

static NSString * const detailID = @"meDetail";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置表头视图
    [self setBackImage];
    
    // 设置导航栏
    [self setupNavigationView];
    
    
}

#pragma mark - 设置导航栏
- (void)setupNavigationView
{
    // 标题
    self.navigationItem.title = @"我的主页";
}

#pragma mark - 设置表头视图
- (void)setBackImage
{
    // 设置表头视图
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 0.3)];
    
    // 表头视图 - 背景图片
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:headView.frame];
    backImageView.image = [UIImage imageNamed:@"topicAvatarDefault1"];
    [headView addSubview:backImageView];
    
    // 背景图片添加轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changBackGroudImage)];
    [backImageView addGestureRecognizer:tap];
    backImageView.userInteractionEnabled = YES;
    self.backImageView = backImageView;
    self.page = 1;
    
    // 表头视图 - 头像
    UIButton *iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    iconBtn.xcm_width = 80;
    iconBtn.xcm_height = 80;
    iconBtn.center = CGPointMake(headView.xcm_width * 0.5, headView.xcm_height * 0.5);
    iconBtn.layer.cornerRadius = iconBtn.xcm_width * 0.5;
    iconBtn.layer.masksToBounds = YES;
    NSString *iconString = [[NSUserDefaults standardUserDefaults] objectForKey:@"icon"];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:iconString]];
    [iconBtn setBackgroundImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
    [headView addSubview:iconBtn];
    
    // 表头视图 - 用户名
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:13];
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"nickname"];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    [nameLabel sizeToFit];
    nameLabel.center = CGPointMake(headView.xcm_width * 0.5, headView.xcm_height * 0.5 + iconBtn.xcm_height * 0.7);
    [headView addSubview:nameLabel];
    
    self.tableView.tableHeaderView = headView;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 44;
    self.tableView.backgroundColor = kBackGroudColor;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MeDetailTableViewCell class]) bundle:nil] forCellReuseIdentifier:detailID];
}

#pragma mark - 切换背景图
- (void)changBackGroudImage
{
    self.page++;
    if (self.page > 5) {
        self.page = 1;
    }
    self.backImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"topicAvatarDefault%zd", self.page]];
}

#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.functionArr.count;
}

#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MeDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:detailID];
    cell.titleLabel.text = self.functionArr[indexPath.row];
    cell.subTitleLabel.text = self.contentArr[indexPath.row];
    return cell;
}

#pragma mark - 懒加载
- (NSMutableArray *)functionArr
{
    if (!_functionArr) {
        _functionArr = [@[@"圈等级", @"糗龄"] mutableCopy];
    }
    return _functionArr;
}

- (NSMutableArray *)contentArr
{
    if (!_contentArr) {
        _contentArr = [@[@"LV4", @"1个月"] mutableCopy];
    }
    return _contentArr;
}

@end
